package com.github.spy.apollo.squirrel.service;

import com.github.spy.apollo.squirrel.common.OrderContext;
import com.github.spy.apollo.squirrel.common.OrderEvent;
import com.github.spy.apollo.squirrel.common.OrderState;
import com.github.spy.apollo.squirrel.entity.Order;
import com.github.spy.apollo.squirrel.spring.OrderStateMachineEngineFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * module name
 *
 * @author spy
 * @version 1.0 2020/8/20
 * @since 1.0
 */
@Slf4j
@Service
public class OrderService {

    @Autowired
    OrderStateMachineEngineFactory orderStateMachineEngineFactory;

    public int submitOrder() {
        Order orderDTO = new Order(OrderState.INIT.name());
        OrderContext orderContext = new OrderContext(orderDTO);
        orderStateMachineEngineFactory.fire(orderContext, OrderEvent.SUBMIT_ORDER);
        return orderContext.order.getId();
    }

    public int pay() {
        Order orderDTO = new Order(OrderState.WAIT_PAY.name());
        OrderContext orderContext = new OrderContext(orderDTO);
        orderStateMachineEngineFactory.fire(orderContext, OrderEvent.PAY);
        return orderContext.order.getId();
    }

}
